package com.example.xm.service.impl;

import com.example.xm.entity.Admin;
import com.example.xm.entity.User;
import com.example.xm.entity.dto.UserDto;
import com.example.xm.exception.SerceiceException;
import com.example.xm.mapper.UserMapper;
import com.example.xm.service.IUserService;
import com.example.xm.utils.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Override
    @Transactional
    public void reg(User user) {
        User userByName= userMapper.selectByName(user.getName());
        if(user.getStatus()==null){

        user.setStatus("1");
        }
//        System.out.println("userByName"+userByName);
        if(userByName!=null){
            throw new SerceiceException(400,"注册失败，用户名已存在");
        }
//        System.out.println("UserServiceImpl"+user);
        userMapper.insert(user);
    }

    @Override
    public String login(String name, String password,String type) {
        User userByAccount = userMapper.selectByName(name);
        if(userByAccount==null){
            throw new SerceiceException(400,"用户名不存在");
        }
        if(!(userByAccount.getPassword().equals(password))){
            throw new SerceiceException(400,"密码不正确");
        }
        if(!(type.equals(String.valueOf(userByAccount.getType())))){
            throw new SerceiceException(400,"账号或密码错误");
        }
        if(userByAccount.getStatus().equals("2")){
            throw new SerceiceException(400,"账号已禁用");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",userByAccount.getId());
        map.put("avatar",userByAccount.getAvatar());
        map.put("name",userByAccount.getName());
        map.put("type",userByAccount.getType());
        String token = JWTUtils.getToken(map);
        return token;
    }
    @Override
    public User weblogin(String name, String password,String type) {
        User userByAccount = userMapper.selectByName(name);
        if(userByAccount==null){
            throw new SerceiceException(400,"用户名不存在");
        }
        if(!(userByAccount.getPassword().equals(password))){
            throw new SerceiceException(400,"密码不正确");
        }
        if(!(type.equals(String.valueOf(userByAccount.getType())))){
            throw new SerceiceException(400,"账号或密码错误");
        }
        if(userByAccount.getStatus().equals("2")){
            throw new SerceiceException(400,"账号已禁用");
        }

        return userByAccount;
    }
    @Override
    public User getInfo(String token) {
        Claims parse = JWTUtils.parse(token);
        int id = (int)parse.get("id");
//        System.out.println(id+"================");
        User user= userMapper.selectByPrimaryKey(id);
        return user;
//        return null;
    }

//    @Override
//    public List<UserEntity> getStudents(String[] split,Integer id) {
//        System.out.println(Arrays.toString(split)+"------------"+id);
//        return null;
//    }

    @Override
    public void delete(int[] intArray) {

        userMapper.delete(intArray);
    }

//    @Override
//    public void update(Admin user) {
//
//        adminMapper.updateByPrimaryKeySelective(user);
//    }
//
    @Override
    public PageInfo<User> list(UserDto dto) {
        PageHelper.startPage(dto.getPage(),dto.getSize());
      List<User> adminList=  userMapper.selectList(dto.getName(),dto.getId(),dto.getType());
        PageInfo<User> adminPageInfo = new PageInfo<User>(adminList);
        return adminPageInfo;
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }
}
